/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package org.jackysoft.entity;

import java.util.Map;








import org.dom4j.Element;
import org.jackysoft.bean.define.EntitySchemas;
import org.jackysoft.util.CollectionUtils;

/**
 * 
 */



public class StepNode extends AbstractNode {
	// ~ Instance fields
	// ////////////////////////////////////////////////////////

	/**
	 * 
	 */
	private static final long serialVersionUID = 8480889969580968568L;

	protected ActionsNode actions;

	protected ExternalPermissionsNode permissions;
	protected PostFunctionsNode postFunction;
	protected PreFunctionsNode preFunctions;

	protected Map<String, String> metaAttributes;
	protected String name;	

	/**
     * 
     */
	public StepNode() {
		super();
	}

	/** sets parent */
	public StepNode(Element element) {
		super(element);

	}

	@Override
	public Element asElement() {
		Element root = elementof("step");

		attr(root, "name", name);
		attr(root, "id", id);
		addmetas(root, this.metaAttributes);
		addchild(root, permissions);
		addchild(root, this.preFunctions);
		addchild(root, this.actions);
		addchild(root, this.postFunction);
		return root;
	}

	
	
	public ActionsNode getActions() {
		return actions;
	}

	
	
	
	public Map<String, String> getMetaAttributes() {
		return metaAttributes;
	}

	public String getName() {
		return name;
	}

	
	
	public ExternalPermissionsNode getPermissions() {
		return permissions;
	}

	
	
	public PostFunctionsNode getPostFunction() {
		return postFunction;
	}

	
	public PreFunctionsNode getPreFunctions() {
		return preFunctions;
	}


	@Override
	protected void init(Element root) {
		this.actions = childof(root, "actions", ActionsNode.class);
		this.permissions = childof(root, "external-permissions",
				ExternalPermissionsNode.class);
		this.postFunction = childof(root, "post-functions",
				PostFunctionsNode.class);
		this.preFunctions = childof(root, "pre-functions",
				PreFunctionsNode.class);
		this.metaAttributes = metas(root);
		this.name = stringAttr(root, "name");
		this.id = intAttr(element, "id");

	}

	public boolean resultsInJoin(JoinNode join) {

		ActionsNode acts = this.getActions();
		if (acts == null)
			return false;
		for (ActionNode an : acts.getActions()) {
			if (an.getResultsNode().getUnconditionalResult().getId() == join
					.getId())
				return true;
			if (!CollectionUtils.isEmpty(an.getResultsNode()
					.getConditionalResults())) {
				for (ConditionsResultNode crl : an.getResultsNode()
						.getConditionalResults()) {
					if (crl.getJoin() == 0)
						continue;
					if (crl.getJoin() == join.getId()) {
						return true;
					}
				}
			}
		}

		return false;
	}

	public void setActions(ActionsNode actions) {
		this.actions = actions;
	}

	public void setMetaAttributes(Map<String, String> metaAttributes) {
		this.metaAttributes = metaAttributes;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPermissions(ExternalPermissionsNode permissions) {
		this.permissions = permissions;
	}

	public void setPostFunction(PostFunctionsNode postFunction) {
		this.postFunction = postFunction;
	}

	public void setPreFunctions(PreFunctionsNode preFunctions) {
		this.preFunctions = preFunctions;
	}

	@Override
	public String tagName() {
		// TODO Auto-generated method stub
		return "step";
	}	

	// ~ Methods
	// ////////////////////////////////////////////////////////////////

}
